<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>数组的reduce方法</h1>

    <script>
        // 数组的reduce方法
        // reduce接收两个参数
        //一个是回调函数，另一个是初始值
        // 回调函数有四个参数:第一个参数是
        // pre
        // cur
        // index
        // arr

        let arr = [1, 2, 3, 4, 5]
        console.log(arr.reduce((a, b, c) => {
            console.log(a, b, c)
            return
        }))


        let str = 'wo ai ni a ni zhi dao ma ni bu zhi dao'
        let result = str.split(' ').reduce((pre, item) => {
            pre[item] ? pre[item]++ : pre[item] = 1
            return pre
        }, {})
        console.log(result);

    </script>
</body>

</html>